package com.bdqn.zhang.dao.impl;

import com.bdqn.zhang.dao.VivorzDao;
import com.bdqn.zhang.dao.impl.dbutil.DButil;
import com.bdqn.zhang.pojo.Vivoproduct;
import com.bdqn.zhang.pojo.Vivorz;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class VivorzDaoimpl extends DButil implements VivorzDao {
    Connection conn = null;
    PreparedStatement ment = null;
    ResultSet resultSet = null;
    @Override
    public int add(Vivorz Vivorz) {
        String sql = "INSERT INTO Vivorz(name,caozuo,gongneng,datass,jg)VALUES(?,?,?,?,?)";
        Object[] obj = {Vivorz.getName(),Vivorz.getCaozuo(),Vivorz.getGongneng(),Vivorz.getDatass(),Vivorz.getJg()};
        return executeUpdate(sql, obj);
    }

    @Override
    public List<Vivorz> getAll(String name,int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivorz> list = new ArrayList();
        String sql = "select * from Vivorz where name=? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, name);
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivorz vivorz = new Vivorz();
                    vivorz.setId(resultSet.getInt(1));
                    vivorz.setName(resultSet.getString(2));
                    vivorz.setCaozuo(resultSet.getString(3));
                    vivorz.setGongneng(resultSet.getString(4));
                    vivorz.setDatass(resultSet.getString(5));
                    vivorz.setJg(resultSet.getString(6));
                    list.add(vivorz);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int getCount(String name) {
        ResultSet resultSet = null;
        PreparedStatement ment = null;
        conn = getConnerction();
        String sql = "select count(1) from Vivorz where name=?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, name);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int del(String id) {
        String sql = "DELETE from Vivorz where id in (?)";
        Object[] obj = {id};
        return executeUpdate(sql, obj);
    }
}
